//
//  QGrabCertificateVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/9/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QGrabCertificateVC.h"
#import "QGrabTopCell.h"
#import "QGrabMedCell.h"
#import "QGrabBottomCell.h"
@interface QGrabCertificateVC ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation QGrabCertificateVC{
    
    UITableView *_tableView;
    
    NSString *_nameStr;
    NSString *_phoneStr;
    NSString *_idCardStr;
    
    UIImage *_image_1;
    UIImage *_image_2;
    UIImage *_image_3;
    
    NSInteger _type;
    
    NSMutableArray *_imageArr;
}

- (void)dealloc{
    
    CLog(@"dealloc");
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];//设置状态栏
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"挖掘机证";
    _imageArr = [NSMutableArray array];
    _nameStr = @"";
    _phoneStr = @"";
    _idCardStr = @"";
    
    [self setupForDismissKeyboard];
    [self createTableView];
    
    UIButton *button = [QViewUtility createButtonWithFrame:CGRectMake(0, kScreenHeight-50, kScreenWidth, 50) font:Ksize17 title:@"提交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [button blueStyle];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction{
    _idCardStr = [_idCardStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if(_nameStr.length == 0){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入姓名" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else if (_phoneStr.length != 11){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确电话" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else if (_idCardStr.length != 18 && _idCardStr.length != 15){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入正确身份证号" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else if (_image_1 == nil){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请上传登记照" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else if (_image_2 == nil){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请上传身份证正面" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        
    }else if (_image_3 == nil){
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请上传身份证背面" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }else{
        [self requestData];
    }
}

- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-50) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"QGrabTopCell" bundle:nil] forCellReuseIdentifier:@"QGrabTopCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"QGrabMedCell" bundle:nil] forCellReuseIdentifier:@"QGrabMedCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"QGrabBottomCell" bundle:nil] forCellReuseIdentifier:@"QGrabBottomCell"];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak QGrabCertificateVC *wSelf = self;
    
    
    if(indexPath.section == 0){
        QGrabTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QGrabTopCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dics = _dics;
        return cell;
    }else if (indexPath.section == 1){
        
        QGrabMedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QGrabMedCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameBlock = ^(NSString *str){
            __strong QGrabCertificateVC *sSelf = wSelf;
            sSelf->_nameStr = str;
            CLog(@"姓名===%@",str);
        };
        cell.phoneBlock = ^(NSString *str){
            __strong QGrabCertificateVC *sSelf = wSelf;
            sSelf->_phoneStr = str;
            CLog(@"电话===%@",str);
        };
        return cell;
    }else{
        QGrabBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QGrabBottomCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.oneBlock = ^(){
            __strong QGrabCertificateVC *sSelf = wSelf;
            [sSelf choosePhotoImage];
            sSelf->_type = 1;
            CLog(@"登记照");
            
        };
        if(_image_1){
            
            cell.image_1 = _image_1;
        }
        cell.twoBlock = ^(){
            __strong QGrabCertificateVC *sSelf = wSelf;
            [sSelf choosePhotoImage];
            sSelf->_type = 2;
            CLog(@"身份证正");
        };
        if(_image_2){
            
            cell.image_2 = _image_2;
        }
        cell.threeBlock = ^(){
            __strong QGrabCertificateVC *sSelf = wSelf;
            [sSelf choosePhotoImage];
            sSelf->_type = 3;
            CLog(@"身份证反");
        };
        cell.idCardBlock = ^(NSString *str){
            __strong QGrabCertificateVC *sSelf = wSelf;
            sSelf->_idCardStr = str;
            CLog(@"身份证号===%@",str);
        };
        if(_image_3){
            
            cell.image_3 = _image_3;
        }
        
        return cell;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){
        
        return 120;
    }else if (indexPath.section == 1){
        
        return 130;
    }else{
        
        return 200;
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_tableView endEditing:YES];
}


- (void)requestData{
    
    [_imageArr removeAllObjects];
    NSData *imgData_1 = UIImageJPEGRepresentation(_image_1, 1);
    if(imgData_1.length>1024*1024*2){
        imgData_1 = UIImageJPEGRepresentation(_image_1, .5);
    }
    [_imageArr addObject:imgData_1];
    
    NSData *imgData_2 = UIImageJPEGRepresentation(_image_2, 1);
    if(imgData_2.length>1024*1024*2){
        imgData_2 = UIImageJPEGRepresentation(_image_2, .5);
    }
    [_imageArr addObject:imgData_2];
    
    NSData *imgData_3 = UIImageJPEGRepresentation(_image_3, 1);
    if(imgData_3.length>1024*1024*2){
        imgData_3 = UIImageJPEGRepresentation(_image_3, .5);
    }
    [_imageArr addObject:imgData_3];
    
    
    CLog(@"请求");
    CLog(@"====%@",_idCardStr);
    NSString *url = [QParamService urlWithAction:@"saveCretificates"];
    NSDictionary *param = [QParamService submitCertifiCateAddUser:_nameStr phone:_phoneStr certificatesNumber:_idCardStr stats:_dics[@"id"]];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] uploadMorePhone:url params:param array:_imageArr completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"提交成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)choosePhotoImage{
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选取", nil];
    [actionSheet showInView:self.view];
    
}

#pragma mark - ActionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    if (buttonIndex == 0) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    } else if (buttonIndex == 1) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

#pragma mark - ImagePicker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = info[UIImagePickerControllerEditedImage];
    if(_type == 1){
        _image_1 = image;
    }else if (_type == 2){
        _image_2 = image;
    }else if (_type == 3){
        _image_3 = image;
    }
    
    [_tableView reloadData];
}



@end
